package com.dss.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.dss.po.Expert;
import com.dss.po.ExpertDoc;
import com.infomanage.bean.FieldBean;
import com.infomanage.dao.BaseDAO;
import com.infomanage.exception.DaoException;
import com.infomanage.po.HibernateSessionFactory;

public class ExpertDAO extends BaseDAO<Expert> {

	public List<Expert> findExpertsByClassId(int classId) throws DaoException {
		FieldBean fieldBean = new FieldBean();
		fieldBean.setName("expertClassId");
		fieldBean.setValue(classId);
		return super.findByField(Expert.class, fieldBean);
	}

	public void removeExpert(int expertId) throws DaoException {
		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			Query query = session.createQuery("delete from " + ExpertDoc.class.getSimpleName() + " where expertId="
					+ expertId);
			query.executeUpdate();
			Expert e = (Expert) session.load(Expert.class, new Integer(expertId));
			session.delete(e);
			tx.commit();
		} catch (Exception e) {
			tx.rollback();
			throw new DaoException(e);
		} finally {
			HibernateSessionFactory.closeSession();
		}
	}
}
